package httpbot;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;

import domain.Census2010;
import domain.Place;
import domain.DomainUtil;

public class DatabaseService {
	
	public static final int IOWA = 19;

	public List<Census2010> getPlacesByState(final int stateId){
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("org.sqlite.JDBC");
			con = DriverManager.getConnection("jdbc:sqlite:C:/Users/Jamison/Documents/wikipedia.s3db");
			stmt = con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM census_2010 WHERE state = '" + stateId + "';");
			return DomainUtil.convertCensus2010(rs);
		} 
		catch (final SQLException e) {
			e.printStackTrace();
		}
		catch (final ClassNotFoundException e) {
			e.printStackTrace();
		}
		finally{
			DbUtils.closeQuietly(con);
			DbUtils.closeQuietly(stmt);
			DbUtils.closeQuietly(rs);
		}
		
		return null;
	}
}
